package com.southcn.nfapp.webflux.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;

import java.util.concurrent.TimeUnit;

/**
 * @author LiuZhulan
 */
@RestController
@RequestMapping("/webflux")
@Slf4j
public class WebFluxController {


    private String createStr() {
        try {
            TimeUnit.SECONDS.sleep(5);
        } catch (InterruptedException e) {
        }
        return "some string";
    }

    @GetMapping("/testSpringMVC")
    public String testSpringMVC(){
        log.info("get1 start");
        String result = createStr();
        log.info("get1 end.");
        return result;
    }

    /**
     * 演示异步非阻塞
     * @return
     */
    @GetMapping("/testSpringFlux")
    public Mono<String> testSpringFlux(){
        log.info("get1 start");
        Mono<String> result = Mono.fromSupplier(() -> createStr());
        log.info("get1 end.");
        return result;
    }
}
